package com.hdyg.sixg.activity.mine;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hdyg.sixg.R;
import com.hdyg.sixg.activity.common.BaseActivity;
import com.hdyg.sixg.adapter.NoticeAdapter;
import com.hdyg.sixg.entry.NoticeBean;
import com.hdyg.sixg.mvp.Contrant.CNotice;
import com.hdyg.sixg.mvp.base.BaseUrl;
import com.hdyg.sixg.mvp.base.GetParamUtil;
import com.hdyg.sixg.mvp.base.SpMsg;
import com.hdyg.sixg.mvp.presenter.PNotice;
import com.hdyg.sixg.ui.CustomLoadMoreView;
import com.hdyg.sixg.util.DataCenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.List;

import butterknife.BindView;

/**
 * @author CZB
 * @describe 公告
 * @time 2019/4/8 15:23
 */
public class NoticeActivity extends BaseActivity implements CNotice.IVNotice {

    @BindView(R.id.ll_top_left)
    LinearLayout llTopLeft;
    @BindView(R.id.tvTopTitle)
    TextView tvTopTitle;
    @BindView(R.id.rv_main)
    RecyclerView rvMain;
    @BindView(R.id.srl_refresh)
    SmartRefreshLayout srlRefresh;

    private NoticeAdapter mAdapter;
    private CNotice.IPNotice mPresenter;
    private int page = 1, allDataSize;
    private List<NoticeBean.NoticeListBean> noticeListBeans;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_recyclerview;
    }

    @Override
    protected void initView() {
        onClickListener();
        tvTopTitle.setText(R.string.notice_title);
        rvMain.setLayoutManager(new LinearLayoutManager(mContext));
    }

    private void onClickListener() {
        srlRefresh.setOnRefreshListener(refreshLayout -> {
            srlRefresh.finishRefresh(2000);//传入false表示刷新失败
            page = 1;
            initData();
        });
        llTopLeft.setOnClickListener(v -> finish());
    }

    @Override
    protected void initData() {
        mPresenter.pGetInit(BaseUrl.NOTICE_URL, GetParamUtil.getNoticeParam(page));
    }

    @Override
    protected void createPresenter() {
        mPresenter = new PNotice(this);
    }

    private void initAdapter() {
        mAdapter = new NoticeAdapter(R.layout.item_notice, noticeListBeans);
        mAdapter.setLoadMoreView(new CustomLoadMoreView());
        rvMain.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            mBundle = new Bundle();
            mBundle.putString(SpMsg.INTENT_STR,mAdapter.getData().get(position).getId());
            startActivity(NoticeDetailActivity.class,mBundle);
        });
        mAdapter.setOnLoadMoreListener(() -> {
            if (mAdapter.getData().size() < allDataSize) {
                page++;
                initData();
            } else {
                mAdapter.loadMoreEnd();
            }
        }, rvMain);
    }

    @Override
    public void vGetInitSuccess(NoticeBean dataBean) {
        noticeListBeans = dataBean.getNotice_list();
        allDataSize = dataBean.getTotal();//总条数
        if (page == 1) {
            if (mAdapter != null) {
                mAdapter.setNewData(noticeListBeans);
            } else {
                initAdapter();
            }
        } else {
            if (noticeListBeans.size() != 0) {
                mAdapter.addData(noticeListBeans);
            }
            mAdapter.loadMoreComplete();
        }
    }
}
